package com.news.news.web;

import com.github.pagehelper.PageInfo;
import com.news.news.modle.Comment;
import com.news.news.modle.Link;
import com.news.news.service.CommentService;
import com.news.news.service.LinkService;
import com.news.news.util.CommonResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by 罗铠威 on 2023/8/29 8:19
 * type:
 */
@RestController
@RequestMapping(path = "/admin/CommentCtl")
public class CommentController {

    @Resource
    CommentService commentService;

    /**
     * 获取链接数据列表
     * @return
     */
    @PostMapping(value = "/list")
    public CommonResult list(@RequestBody Comment comment) {
        PageInfo<Comment> commentTypePageInfo = commentService.CommentList(comment);//实现分页功能
        CommonResult commonResult=new CommonResult(200,commentTypePageInfo,"成功");
        return commonResult;
    }


    /**
     * 保存新增链接信息
     * @return
     */
    @RequestMapping(value = "/saveComment")
    public CommonResult saveNews(@RequestBody Comment comment, @RequestHeader("adminToken") String token) {
        if (comment.getId()!= null) {//【修改】
            commentService.updateComment(comment,token);
        } else {//【新增】
            commentService.saveComment(comment,token);
        }
        CommonResult commonResult = new CommonResult(200, null, "成功");

        return commonResult;
    }

    /**
     * 修改友情链接状态
     * @param comment
     * @return
     */
    @RequestMapping(value = "/UpdateCommentState")
    public CommonResult UpdateNewsTypeState(@RequestBody Comment comment) {

        commentService.UpdateLinkState(comment);
        CommonResult commonResult = new CommonResult(200, null, "修改状态成功");

        return commonResult;
    }

    /**
     * 【编辑前显示数据】
     * @param id 链接的ID，通过它来获取信息返回前端
     * @return
     */
    @RequestMapping(value = "/findCommentById")
    public CommonResult findLinkById(Integer id) {

        Comment comment=commentService.findCommentById(id);
        CommonResult commonResult = new CommonResult(200, comment, "修改状态成功");

        return commonResult;
    }


    /**
     * 【删除数据】
     * @param id 链接的ID，通过它来删除数据
     * @return
     */
    @RequestMapping(value = "/deleteCommentById")
    public CommonResult deleteCommentTypeById(Integer id) {

        commentService.deleteCommentById(id);
        CommonResult commonResult = new CommonResult(200, null, "修改状态成功");

        return commonResult;
    }


}
